<?php
$page = 'admin';
include '../database/connect.php';
$lesson_id = $_GET['lesson_id'];
$page_no = (int) (isset($_GET['page_no']) ? $_GET['page_no'] : "0");
if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "INSERT INTO lesson_pages SET content='$content', lesson_id='$lesson_id',added_by='$added_by'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?lesson_id=$lesson_id");
}
if (isset($_GET['update'])) {
    session_start();
    extract($_POST);
    $page_id = $_GET['page_id'];
//    extract($_GET);
    $added_by = $_SESSION['username'];
    $query = "UPDATE lesson_pages SET content='$content_update' WHERE id='$page_id'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?lesson_id=$lesson_id");
}

require_once '../views/header.php';
$query = "SELECT * FROM lessons where id='$lesson_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_array($result);
$topic = $row['topic'];
$course_id=$row['course'];

//lets get the course name for purpose of navigation
$query = "SELECT * FROM courses where id='$course_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_assoc($result);
$course=$row['course'];

?>
<script>
    function showAddNew(){
        hideEditPage();
        $('#add_new_page').show('slow');
    }
    function hideAddNew(){
        $('#add_new_page').hide('slow');
    }
    function hideEditPage(){
        $('#edit_page').hide('slow');
    }
    function editPage(lesson_id,page_id){
        hideAddNew();
        //        prompt("sd","edit_page.php?lesson_id="+lesson_id+"&page_id="+page_id);
        $('#edit_page').load("edit_page.php?lesson_id="+lesson_id+"&page_id="+page_id,function(){            
            $('#edit_page').show('slow');
        });
    }
    function update(){
        document.getElementById("update_page_form").submit();
    }
    
    jQuery(document).ready(function(){
        jQuery("#page_form").validationEngine();
    });
</script>

<p>
<a href="admin_home.php">Admin home</a>&gt;&gt;<a href='courses.php'>Courses</a>&gt;&gt;
<a href="lessons.php?course_id=<?php echo $course_id; ?>"><?php echo $course; ?></a>&gt;&gt;<?php echo $topic; ?>
</p>
<div id="edit_page" style="display: none"></div>
<div id="add_new_page" style="display: none">
    <form action="?save=1&lesson_id=<?php echo $lesson_id; ?>" method="post" id="page_form"/>
    <table>
        <tr>
            <th>
                <?php
                echo 'Topic: ' . $topic;
                ?>
            </th>
        </tr>
        <tr>
            <td><textarea name="content" id='content' cols="60" rows="17" class="validate[required,minSize[30],maxSize[1000]]"></textarea></td>
        </tr>
        <tr>
            <td><input type="submit" value="Add Page" /> <input type="button" value="Hide" onclick="hideAddNew();" /></td>
        </tr >
    </table>
</form>

</div>
<a href="javascript:showAddNew();">Add a page</a>

<?php
$query = "SELECT count(*) as count FROM lesson_pages where lesson_id='$lesson_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_assoc($result);
$page_count = $row['count'];
if ($page_count == 0) {
    echo "No lesson in the database";
} else {
    $query = "SELECT * FROM lesson_pages where lesson_id='$lesson_id' limit $page_no, 1";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    ?>
    <table class="table">
        <?php
        $row = mysql_fetch_assoc($result);
        echo "<tr><td> " . nl2br($row['content']) . " 
<a href='edit_page.php?lesson_id=$lesson_id&page_id=" . $row['id'] . "'>Edit page</a>
    </td></tr>";
        ?>
    </table>
    <div style="clear: both"></div>
    Page <?php echo $page_no + 1 ?> of <?php echo $page_count ?> 
    <?php if (($page_no + 1) < $page_count) { ?>
        <a href='<?php echo "?lesson_id=$lesson_id&page_no=" . ($page_no + 1); ?>'>Next</a>
    <?php } else { ?>
        Next
        <?php
    }
    if ($page_no > 0) {
        ?>
        <a href='<?php echo "?lesson_id=$lesson_id&page_no=" . ($page_no - 1); ?>'>Prev</a>    
    <?php } else { ?>
        Prev
    <?php } ?>
    <br/>
    <?php
    for ($count = 0; $count < $page_count; $count++) {
        if ($count == (int) $page_no) {
            echo ($count + 1);
        } else {
            echo "<a href='?lesson_id=$lesson_id&page_no=$count'> " . ($count + 1) . " </a>";
        }
    }
}
?>
<?php require_once '../views/footer.php'; ?>